//
//  VideoPlayViewController.m
//  XMDLearn
//
//  Created by 张硕 on 16/4/16.
//  Copyright © 2016年 hao. All rights reserved.
//

#import "VideoPlayViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
@interface VideoPlayViewController ()
@property(strong,nonatomic)AVPlayerViewController *avPlayerVC;
@property(strong,nonatomic)UISegmentedControl *SC;
@property(strong,nonatomic)UITextView *detailsTextView;
@property(strong,nonatomic)UITableView *vedioTableView;
@property(strong,nonatomic)NSURL *videoUrl;
@property(strong,nonatomic)AVPlayer *player;
@property(strong,nonatomic)AVPlayerViewController *playerVC;

@end

@implementation VideoPlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _playerVC = [[AVPlayerViewController alloc]init];
    _playerVC.view.frame =CGRectMake(0,64, self.view.frame.size.width,self.view.frame.size.height*0.2);
    [self.view addSubview:_playerVC.view];
    
    [self videoPlay];
    [self selected];
    [self chooseDetails];
}
-(void)videoPlay{
    _videoUrl = [NSURL URLWithString:@"http://7xj11m.com1.z0.glb.clouddn.com/2016-01-14_56974386138b7.mp4"];
    _player = [AVPlayer playerWithURL:_videoUrl];
     _playerVC.player =_player;
    self.avPlayerVC = _playerVC;
    [self.avPlayerVC.player play];
}
-(void)selected{
    _SC = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height*0.45, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height*0.05)];
    UIColor *MyTint = [[UIColor alloc] initWithRed:0.66 green:1.0 blue:0.77 alpha:1.0];
    
    _SC.tintColor = MyTint;
    _SC.backgroundColor = [UIColor grayColor];
    _SC.selectedSegmentIndex = 0;
    [_SC insertSegmentWithTitle:@"First" atIndex:0 animated:YES];
    [_SC insertSegmentWithTitle:@"One" atIndex:1 animated:YES];
    [_SC addTarget:self action:@selector(choose) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_SC];
}
-(void)choose{
    NSInteger Index = _SC.selectedSegmentIndex;
    switch (Index) {
        case 0:
            [self chooseDetails];
            break;
        case 1:
            [self chooseVedio];
            break;
        default:
            break;
    }
}
-(void)chooseDetails{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 10;    //行间距
    paragraphStyle.maximumLineHeight = 60;   /**最大行高*/
    paragraphStyle.firstLineHeadIndent = 20.f;    /**首行缩进宽度*/
    paragraphStyle.alignment = NSTextAlignmentJustified;
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:20],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    
    _detailsTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height*0.51, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height*0.49)];
    _detailsTextView.backgroundColor =[UIColor grayColor];
    _detailsTextView.text = @"sdfsffsfdsfsfdsfsfsdfsfssgdsgjwekfjjnvdfklsjnklvndsdfsafswfsdfsdfsafsfsafsfasfsfasfsfsfsfsfsfas";
    [_detailsTextView setEditable:NO];
    [_detailsTextView setSelectable:NO];
    _detailsTextView.attributedText = [[NSAttributedString alloc]initWithString:_detailsTextView.text attributes:attributes];
    [self.view addSubview:_detailsTextView];
}
-(void)chooseVedio{
    _vedioTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height*0.51, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height*0.49)];
    _vedioTableView.backgroundColor = [UIColor grayColor];
    _vedioTableView.delegate = self;
    _vedioTableView.dataSource = self;

    [self.view addSubview:_vedioTableView];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifier = @"videoPlayIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return  cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"sdfsfsds");
    [self videoPlay];
}
- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}
@end
